<template>
  <div>
    <el-form label-width="70px" :inline="true" class="login-container" ref="form" :model="form" :rules="rules">
      <h3 class="login_title">系统登录</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="button">
          <el-button type="primary" @click="submit">登录</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import  Mock  from 'mockjs'
import Cookie from 'js-cookie'
import { getMenu } from '../api'
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods:{
    //登录
    submit(){
    //token信息
    // const token = Mock.Random.guid()
    

    //校验通过
    this.$refs.form.validate((valid) => {
       if(valid){
        getMenu(this.form).then(({data}) =>{
          console.log(data);
          if(data.code === 20000){
            // token信息存入cookie用于不同页面的通信
             Cookie.set('token',data.data.token)
             //获取菜单数据，存入vuex中
             this.$store.commit('setMenu',data.data.menu)
             //动态获取router
             this.$store.commit('addMenu',this.$router)
             //跳转到首页
             this.$router.push('/home')
             this.$message.success('登录成功')
          }else{
            this.$message.error(data.data.message)
          }
        })
      }
    })
     


    }
    
    }
  }

</script>
<style lang="less" scoped>
.login-container {
  width: 350px;
  border: 1px solid #eaeaea;
  margin: 50px auto;
  padding: 35px 35px 35px 35px;
  background-color: #fff;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
  box-sizing: border-box;

  .el-input {
    width: 150px;
  }
  .login_title {
    text-align: center;
    margin-bottom: 40px;
  }
  .button {
    width: 280px;
    // background-color: #cac6c6;
    text-align: center;
  }
}
</style>